home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / MATHS / RLAB / RLAB125.ZIP / !RLaB / toolbox / gamma < prev    next >
Text File  |  1994-02-21  |  5KB  |  218 lines

  1. //-------------------------------------------------------------------//
  2. //
  3. // Syntax:    gamma ( A )
  4. //        gammaln ( A )
  5. //        gammp ( A , X )
  6. //        gammq ( A , X )
  7.  
  8. // Description:
  9.  
  10. // gamma:    Computes the value of the gamma function. The input, A
  11. //        must be a row, or column vector. Allowable values of A
  12. //        are: A > 0.  
  13.  
  14. // gammln:    Computes the value of the log-gamma function.
  15.  
  16. // gammp:    Computes the value of the incomplete gamma function,
  17. //        P(a,x). 
  18. //         Reference: Numerical Recipes in C, pp 172.
  19.  
  20. // gammq:    Computes the value of the incomplete gamma function,
  21. //        Q(a,x) = 1 - P(a,x). 
  22. //         Reference: Numerical Recipes in C, pp 173.
  23.  
  24. //-------------------------------------------------------------------//
  25.  
  26.  
  27. static (gser, gcf);
  28.  
  29. gamma = function( A )
  30. {
  31.   if (!all (all (A >= 0))) { error ("valid range for gamma() 0:inf"); }
  32.   return exp( gammaln( A ) );
  33. };
  34.  
  35. gammaln = function( A )
  36. {
  37.   local(a, cof, j, ser, tmp);
  38.  
  39.   if( !all (all (A >= 0)) )    // Test for correct input range
  40.   { 
  41.     return inf(); 
  42.   }
  43.  
  44.   cof = [76.18009173, -86.50532033, 24.01409822, ...
  45.          -1.231739516, 0.120858003e-2, -0.536382e-5];
  46.  
  47.   a = A - 1;
  48.   tmp = a + 5.5;
  49.   tmp = tmp - (a + 0.5) .* log(tmp);
  50.   ser = 1;
  51.   for( j in 1:6 )
  52.   {
  53.     a = a + 1;
  54.     ser = ser + cof[j]./a;
  55.   }
  56.   return ( -tmp + log(2.50662827465*ser) );
  57. };
  58.  
  59. //
  60. // Computes the value of the incomplete gamma function, P(a,x).
  61. // Currently the input, A, must be a row, or column matrix.
  62. //
  63.  
  64. gammp = function( X, A )
  65. {
  66.   local(a, gammcf, gammser, gln, g, i);
  67.  
  68.   if( any (any (X < 0)) || any (any (A < 0))) {
  69.     error ("invalid arguments to gammp()");
  70.   }
  71.  
  72.   if (!all (size (X) == size (A)))
  73.   {
  74.     if (A.n != 1) 
  75.     { 
  76.       error ("size of 2nd arg must match 1st, or be 1-by-1");
  77.     else
  78.       a = A.*ones (size (X));
  79.     }
  80.   else
  81.     a = A;
  82.   }
  83.   g = zeros (size (X));
  84.   for (i in 1:X.n)
  85.   {
  86.     if(X[i] < (a[i] + 1))
  87.     {
  88.       g[i] =  gser (a[i], X[i], gln);
  89.     else
  90.       g[i] =  1 - gcf (a[i], X[i], gln);
  91.     }
  92.   }
  93.   return g;
  94. };
  95.  
  96. //
  97. // Computes the value of the incomplete gamma function, 
  98. // Q(a,x) = 1 - P(a,x). 
  99. //
  100.  
  101. gammq = function( X , A )
  102. {
  103.   local(a, gammcf, gammser, gln, g, i);
  104.  
  105.   if(any (any (X < 0)) || any (any (A <= 0))) {
  106.     error ("invalid arguments to gammq()");
  107.   }
  108.  
  109.   if (!all (size (A) == size (X)))
  110.   {
  111.     if (A.n != 1) 
  112.     { 
  113.       error ("size of 2nd arg must match 1st, or be 1-by-1");
  114.     else
  115.       a = A.*ones (size (X));
  116.     }
  117.   else
  118.     a = A;
  119.   }
  120.   g = zeros (size (X));
  121.   for (i in 1:X.n)
  122.   {
  123.     if(X[i] < (a[i] + 1))
  124.     {
  125.       g[i] = 1 - gser ( a[i], X[i], gln );
  126.     else
  127.       g[i] = gcf ( a[i], X[i], gln );
  128.     }
  129.   }
  130.   return g;
  131. };
  132.  
  133. //-------------------------------------------------------------------//
  134. //
  135. // Syntax:    gser ( A, X, GLN )
  136.  
  137. // Description:
  138.  
  139. // Computes the value of the incomplete gamma function, evaluated by
  140. // it's series representation.
  141.  
  142. // Reference: Numerical Recipes in C, pp 173.
  143. //-------------------------------------------------------------------//
  144.  
  145. gser = function( A, X, GLN )
  146. {
  147.   local(ITMAX, ap, del, eps, n, sum);
  148.  
  149.   GLN = gammaln(A);
  150.   if( any (any(X <= 0)))
  151.   {
  152.     if( any( X < 0 ) ) {
  153.       error("X less than 0 in function gser()");
  154.     }
  155.     return 0;
  156.   else
  157.     ITMAX = 100; eps = epsilon();
  158.     ap = A;
  159.     del = sum = 1./A;
  160.     for( n in 1:ITMAX )
  161.     {
  162.       ap = ap + 1;
  163.       del = del .* X./ap;
  164.       sum = sum + del;
  165.       if( all (all(abs (del) < abs (sum).*eps)))
  166.       {
  167.         return sum.*exp(-X + A.*log(X) - GLN);
  168.       }
  169.     }
  170.     error("A too large, ITMAX too small in function gser()");
  171.     return 0;
  172.   }
  173. };
  174.  
  175. //-------------------------------------------------------------------//
  176. //
  177. // Syntax:    gcf ( A, X, GLN )
  178.  
  179. // Description:
  180.  
  181. // Computes the value of the incomplete gamma function, evaluated by
  182. // it's continued fraction representation.
  183.  
  184. // Reference: Numerical Recipes in C, pp 174.
  185. //-------------------------------------------------------------------//
  186.  
  187. gcf = function( A, X, GLN )
  188. {
  189.   local(ITMAX, a0, a1, an, ana, anf, b0, b1, eps, fac, g, gold, n);
  190.  
  191.   ITMAX = 100;
  192.   a0 = 1; b0 = 0; b1 = 1; fac = 1; gold = 0;
  193.   eps = epsilon();
  194.   GLN = gammaln(A);
  195.   a1 = X;
  196.   for( n in 1:ITMAX )
  197.   {
  198.     an = n;
  199.     ana = an - A;
  200.     a0 = (a1 + a0.*ana).*fac;
  201.     b0 = (b1 + b0.*ana).*fac;
  202.     anf = an.*fac;
  203.     a1 = X.*a0 + anf.*a1;
  204.     b1 = X.*b0 + anf.*b1;
  205.     if( all(a1) )
  206.     {
  207.       fac = 1./a1;
  208.       g = b1.*fac;
  209.       if( abs((g - gold)/g) < eps )
  210.       {
  211.         return exp(-X + A.*log(X) - GLN).*g;
  212.       }
  213.     gold = g;
  214.     }
  215.   }
  216.   error("A too large, ITMAX too small in function gcf()");
  217. };
  218.